package com.xiangxiangjd.controller;

import com.xiangxiangjd.bean.AdminUser;
import com.xiangxiangjd.controller.helper.$;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.TrUserService;
import com.xiangxiangjd.bean.TrUser;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2022-09-19
 */
@Controller
@RequestMapping("/api/trUser")
@Api(tags = "用户表")
public class TrUserController extends BaseController{
    @Autowired
    TrUserService trUserService;



     /** 主页面 */
    @GetMapping("admin/main.html")
    public String main(){
       return  "TrUser/TrUserMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @GetMapping(value = "admin/addTrUserHtml")
    public String addTrUserHtml( ){
             return "TrUser/TrUserAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @GetMapping(value = "admin/editTrUserHtml")
    public String editTrUserHtml(String id ,Model model ){
           model.addAttribute("trUser",trUserService.selectTrUserByIdTrUser(id));
             return "TrUser/TrUserEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @PostMapping(value = "admin/showTrUserHtml")
    public String showTrUserHtml(String id ,Model model ){
           model.addAttribute("trUser",trUserService.selectTrUserByIdTrUser(id));
             return "TrUser/TrUserShow";
    }


    /** 分页查询TrUser
     * @param page  页码
     * @param limit  查几条
     * @param trUser  前端传进的参数
     * @return 返回统一分页信息
     */
    @GetMapping(value = "admin/findTrUserByPage")
    @ResponseBody
    @ApiOperation(value="分页查询", notes="分页查询", produces="application/json")
    // @PreAuthorize("hasRole('ROLE_USER')")
    public ResultPage findTrUserByPage(Integer page, Integer limit,TrUser trUser){
             return trUserService.findTrUserByPage(page,limit,trUser);
    }

    /** 添加TrUser
     * @param trUser  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/addTrUser")
    @ResponseBody
    @ApiOperation(value="添加", notes="添加", produces="application/json")
    // @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel saveTrUser(TrUser trUser  ){
             return trUserService.addTrUser(trUser);
    }

    /** 编辑TrUser
     * @param trUser  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/editTrUser")
    @ResponseBody
    @ApiOperation(value="编辑", notes="编辑", produces="application/json")
    // @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel editTrUser(TrUser trUser  ){
             return trUserService.editTrUser(trUser);
    }

    /** 删除TrUser
     * @param ids  前端传进的ID
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/delTrUser")
    @ResponseBody
    @ApiOperation(value="查询自己的信息", notes="查询自己的信息", produces="application/json")
   // @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel delTrUser(@RequestParam("datas") ArrayList ids){


             return trUserService.delTrUsers(ids);
    }


    /**
     *
     * 查询自己的信息
     * @param   前端传进的ID
     * @return 返回统一信息
     */
    @GetMapping(value = "/getMyInfo")
    @ResponseBody
    @ApiOperation(value="查询自己的信息", notes="查询自己的信息", produces="application/json")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel getMyInfo(){

        AdminUser user = getUser();

        return $.success(user);
    }



    /**
     *
     * 查询所有人的信息
     * @param ids  前端传进的ID
     * @return 返回统一信息
     */
    @GetMapping(value = "/getAllUser")
    @ResponseBody
    @ApiOperation(value="查询所有用户的信息", notes="查询所有用户的信息", produces="application/json")
     @PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
    public ApiResultModel getAllUser(){

        return $.success(trUserService.list());
    }



}
